package com.example.sunsg.okhttptest.viewholder;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.util.Util;

/**
 * Created by sunsg on 16/3/9.
 *
 * layout R.layout.item_activity_spot_edit_description
 */
public class SpotEditItemDescriptionHolder extends RecyclerView.ViewHolder{
    private EditText mEtDescription;
    private TextView mTvTip;
    public SpotEditItemDescriptionHolder(View itemView) {
        super(itemView);
        initView(itemView);
        bindEvent();
    }

    private void initView(View view){
        mEtDescription = (EditText) view.findViewById(R.id.et_description);
        mTvTip = (TextView) view.findViewById(R.id.tv_tip);


    }

    private void bindEvent(){

        mTvTip.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                showEditText();
            }
        });

        mEtDescription.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    hideEditText();
                }
            }
        });
    }

    /**
     * 显示输入框
     */
    public void showEditText(){
        mEtDescription.setVisibility(View.VISIBLE);
        mTvTip.setVisibility(View.GONE);
        mEtDescription.requestFocus();
        mEtDescription.setFocusable(true);
        Util.showSoftInput(mEtDescription);
    }

    /**
     * 没有内容 隐藏输入框
     */
    public void hideEditText(){
        if(TextUtils.isEmpty(mEtDescription.getText().toString())){
            mEtDescription.setVisibility(View.GONE);
            mTvTip.setVisibility(View.VISIBLE);
            mTvTip.requestFocus();
            mTvTip.setFocusable(true);
        }
        Util.hideSoftInputForce(mEtDescription);
        Log.i("test","hide softinput");
    }
}
